#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setCentralWidget(ui->plainTextEdit);

    initUI();
    initSignalSlot();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initUI()
{
    curFilePath = new QLabel;
    curFilePath->setText("当前文件：");
    progressBar = new QProgressBar;
    progressBar->setValue(20);

    ui->statusbar->addWidget(curFilePath);
    ui->statusbar->addWidget(progressBar);

    fontSizeSpin = new QSpinBox;
    fontSizeSpin->setMinimum(9);
    fontSizeSpin->setMaximum(50);

    ui->toolBar->addWidget(new QLabel("字体大小"));
    ui->toolBar->addWidget(fontSizeSpin);

    fontFamilyCombo = new QFontComboBox;
    ui->toolBar->addWidget(new QLabel("字体"));
    ui->toolBar->addWidget(fontFamilyCombo);


}

void MainWindow::initSignalSlot()
{
    connect(fontSizeSpin, SIGNAL(valueChanged(int)), this, SLOT(on_fontSizeSpin_valueChanged(int)));
}


void MainWindow::on_actionTextBold_triggered(bool checked)
{
//    QFont font = ui->plainTextEdit->font();
//    font.setBold(checked);
//    ui->plainTextEdit->setFont(font);

    QTextCharFormat fmt = ui->plainTextEdit->currentCharFormat();
    fmt.setFontWeight(checked ? QFont::Bold : QFont::Normal);
    ui->plainTextEdit->setCurrentCharFormat(fmt);

}

void MainWindow::on_plainTextEdit_copyAvailable(bool b)
{
    ui->actionCut->setEnabled(b);
    ui->actionCopy->setEnabled(b);
    ui->actionPast->setEnabled(b);
}

void MainWindow::on_plainTextEdit_selectionChanged()
{
    QTextCharFormat fmt = ui->plainTextEdit->currentCharFormat();
    ui->actionTextBold->setChecked(fmt.font().bold());
}

void MainWindow::on_fontSizeSpin_valueChanged(int size)
{
    QTextCharFormat fmt;
    fmt.setFontPointSize(size);
    ui->plainTextEdit->mergeCurrentCharFormat(fmt);
}
